<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
				</div>
				<div class="flex-end-center-wrap">
					<el-checkbox
						v-model="isCustomDate"
						border
						class="item-gap"
						size="small"
					>自定义</el-checkbox>
					<el-radio-group
						v-if="!isCustomDate"
						v-model="dateType"
						size="small"
						class="item-gap"
						@change="onDateTypeChange"
					>
						<el-radio-button
							v-for="(dateTypeItem) in dateTypeList"
							:key="dateTypeItem.id"
							:label="dateTypeItem.id"
						>{{ dateTypeItem.name }}</el-radio-button>
					</el-radio-group>
					<jk-date-picker
						v-else
						v-model="searchParams.belongDate"
						type="daterange"
						:picker-options="pickerOptions"
						:clearable="true"
						start-placeholder="计划开始日期"
						end-placeholder="计划结束日期"
						class="search-bar-item-size4 margin-5-0 item-gap"
					></jk-date-picker>
					<jk-select
						v-model="searchParams.workshopId"
						:clearable="false"
						:option-list="workshopList"
						placeholder="区域"
						class="search-bar-item-size1 margin-5-0 item-gap"
					></jk-select>
					<jk-select
						v-model="searchParams.upkeepTypeId"
						:clearable="true"
						:option-list="upkeepTypeList"
						placeholder="保养类别"
						class="search-bar-item-size1 margin-5-0 item-gap"
					></jk-select>
					<jk-search-input
						v-model="searchParams.upkeepItemName"
						placeholder="保养项目"
						size="small"
						class="search-bar-item-size2 margin-5-0 item-gap"
						clearable
						@on-enter="onSearchBtn"
					/>
					<jk-search-input
						v-model="searchParams.nameOrCode"
						placeholder="设备编号或名称"
						size="small"
						class="search-bar-item-size2 margin-5-0 item-gap"
						clearable
						@on-enter="onSearchBtn"
					/>
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table
				ref="jkTable"
				:loading="tableLoading"
				:data="tableData"
				:height="tableHeight + 20"
			>
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<jk-state-bar v-if="item.keyName === 'upkeepState'" :state="scope.row.upkeepState" :state-list="upkeepStateList"></jk-state-bar>
						<span v-else-if="item.keyName === 'cycleUnit'">{{ transferCircleUnit(scope.row) }}</span>
						<span v-else-if="item.keyName === 'empName'">{{ scope.row.itemList && scope.row.itemList.length ? scope.row.itemList[0].empName : '' }}</span>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
				<!--<vxe-column title="操作" align="center" fixed="right" :width="120">
					<template slot-scope="scope">
						<div class="flex-center">
							<div class="item-gap"><el-button size="mini" type="warning" @click="onClearFinishBtn(scope.row)">撤销完成</el-button></div>
						</div>
					</template>
				</vxe-column>-->
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
	</div>
</template>

<script>
    import { auditStateList } from '@/utils/jk/status-option-list';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, formatDate, tipsBar } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../../mixins/getMenuColumn';
    import handlerCache from '../../../../mixins/handler-cache';
    import {
        machineManagerUpkeepMachineJobClearComplete,
        machineManagerUpkeepMachineJobPageRecord
    } from '../../../../api/machine/upkeepTask';
    import { upkeepStateList } from '../../../../utils/jk/status-option-list';
    import upkeepMixin from '../upkeepItemPlan/upkeep';
    import { machineManagerUpkeepTypePage } from '../../../../api/machine/upkeepCategory';
    export default {
        name: 'UpkeepRecord',
        components: {
        },
        mixins: [calHeight, getMenuColumn, handlerCache, upkeepMixin],
        data() {
            return {
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                upkeepTypeList: [],
                upkeepStateList: upkeepStateList,
                prefix: '',
                apiItem: {
                    'page': machineManagerUpkeepMachineJobPageRecord
                },
                dateTypeList: [
                    { id: 14, name: '最近两周' },
                    { id: 7, name: '最近一周' },
                    { id: 1, name: '最近两天' },
                    { id: 0, name: '今天' },
                    { id: -1, name: '所有' }
                ],
                workshopList: [],
                importDialogState: false,
                auditStateList: auditStateList,
                dataId: null,
                tableLoading: true,
                exportLoading: false,
                tableData: [],
                totalCount: 0,
                isCustomDate: false,
                dateType: -1,
                searchParams: {
                    pageNo: 1,
                    pageSize: pageConfig.pageSize,
                    auditState: 1,
                    nameOrCode: '',
                    upkeepTypeId: null,
                    belongDate: []
                }
            };
        },
        methods: {
            onClearFinishBtn(row) {
                this.$msgbox({
                    title: '消息',
                    message: `确定"撤销完成"吗？`,
                    iconClass: 'el-icon-question',
                    showCancelButton: true,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    beforeClose: (action, instance, done) => {
                        if (action === 'confirm') {
                            instance.confirmButtonLoading = true;
                            instance.confirmButtonText = '执行中...';
                            machineManagerUpkeepMachineJobClearComplete({
                                id: row.id
                            }).then(res => {
                                instance.confirmButtonLoading = false;
                                instance.confirmButtonText = '确定';
                                if (res.data.status === 200) {
                                    done();
                                    this.getListRequest();
                                }
                            });
                        } else {
                            done();
                        }
                    }
                });
            },
            onDateTypeChange(e) {
                if (e === -1) {
                    this.$set(this.searchParams, 'belongDate', []);
                } else {
                    this.$set(this.searchParams, 'belongDate', [formatDate(Date.now() - (e * 24 * 60 * 60 * 1000)) + ' 00:00:00', formatDate(Date.now()) + ' 23:59:59']);
                }
            },
            onVisibleChangeOfImportDialog(e) {
                this.importDialogState = e;
            },
            onFinishBtnOfImportDialog() {
                this.importDialogState = false;
                this.getListRequest();
            },
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                this.apiItem.page(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.totalCount = 1;
                this.getListRequest();
            },

            /** 删除按钮操作 */
            onDeleteBtn(row) {
                this.apiItem.delete({ id: row.id }).then((res) => {
                    if (res.data.status === 200) {
                        this.getListRequest();
                        this.$tipsBar(this, 'delete');
                    }
                }).catch(() => {});
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            getUpkeepTypeListRequest() {
                return machineManagerUpkeepTypePage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.upkeepTypeList = res.data.res || [];
                    }
                });
            },
            async getDependentData() {
                this.onDateTypeChange(this.dateType);
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                await this.getUpkeepTypeListRequest();
                await this.getMenuColumnListRequest();
                await this.getListRequest();
            }
        },
        mounted() {
            this.getDependentData();
        }
    };
</script>
